<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>Class inheritance via javascript</title>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="js-inheritance-demo">
			<h2>1,Prototypal Inheritance-By Crockford=>Add a static method 'create' to the Object</h2>
			<div class="row">
				<pre class="brush:js;">
				if(typeof(Object.create)!=='function'){
				  Object.create=function(srcObj,extra){
				    var F=function(){},udf;
				    F.prototype=srcObj;
				    
				    //Extra properties or methods
				    for(var p in extra||{}){
				      if(!p) continue;
				      F.prototype[p]=extra[p];
				    };
				    
				    //Return the extended object
				    
				    return new F();
				  };
				};
				
				var classA=function(name){
				  this.name=name;
				};
				
				var srcObj=new classA("levin");
				
				var obj=Object.create(srcObj,{
				  sayHi:function(){
				    alert("My name is "+this.name);
				  }
				});
				
				obj.sayHi();
				
				alert((obj instanceof classA));//will return true
				</pre>
			</div>
			<h2>2,Inheritance via JQuery,more like clone=>$.extend({},{src},{extra})</h2>
			<div class="row">
				<pre class="brush:js">
					var classA=function(name){
					    this.name=name;
					};
					var srcObj=new classA("levin");
					var inherittedObj=$.extend({},srcObj,{
						sayHi:function(){alert("My name is "+this.name);}
					});
					
					inherittedObj.sayHi();
					
					alert(inherittedObj instanceof classA);//will return false;
					
				</pre>
			</div>
		</div>
	</body>
</html>
